import { registerMicroApps, start, initGlobalState, MicroAppStateActions } from 'qiankun';

let visuallyEntry = '//yunfeng.icu:8091/visually/';
let lowcodeEntry = '//yunfeng.icu:8091/lowcode/';
let communicateEntry = '//yunfeng.icu:8091/communicate/';
if (import.meta.env.DEV) {
    visuallyEntry = '//localhost:5174/visually/';
    lowcodeEntry = '//localhost:5175/lowcode/';
    communicateEntry = '//localhost:5176/communicate/';
}
const container = '#sub-app'

// 1. 要加载的子应用列表
const apps = [
    // {
    //   name: "sub-react", // 子应用的名称
    //   entry: '//localhost:3001', // 默认会加载这个路径下的html，解析里面的js
    //   activeRule: "/sub-react", // 匹配的路由
    //   container: "#sub-app" // 加载的容器
    // },
    {
        name: "visually",
        entry: visuallyEntry,
        activeRule: "/visually",
        container
    },
    {
        name: "lowcode",
        entry: lowcodeEntry,
        activeRule: "/lowcode",
        container
    },
    {
        name: "communicate",
        entry: communicateEntry,
        activeRule: "/communicate",
        container
    },
]


// 2. 注册子应用
registerMicroApps(apps, {
    beforeLoad: [async app => console.log('before load', app.name)],
    beforeMount: [async app => console.log('before mount', app.name)],
    afterMount: [async app => console.log('after mount', app.name)],
})


// 初始化 state
// const actions: MicroAppStateActions = initGlobalState({ chenyf: 1234 });

// actions.onGlobalStateChange((state, prev) => {
//     // state: 变更后的状态; prev 变更前的状态
//     console.log(2, state, prev);
// });

// setTimeout(() => {
//     actions.setGlobalState({ chenyf: 124 });
// }, 1000);
// actions.offGlobalStateChange();

// 启动 qiankun
// start();
export default start;